package com.droid.lib.controls.widget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.droid.lib.controls.R;

import java.util.ArrayList;
import java.util.List;

public class RadarPopAdapter extends BaseAdapter {
    private List<RadarPopItem> radarPopItems = new ArrayList<>();
    private Context mContext;

    public RadarPopAdapter(Context mContext) {
        this.mContext = mContext;

    }

    public void setRadarPopItems(List<RadarPopItem> radarPopItems) {
        this.radarPopItems = radarPopItems;
        notifyDataSetChanged();
    }

    public void setData(int parent, int child, List<XRadarView.RedarItem> mlistRedarItems) {
        if (child == -1) {
            for (int i = 0; i < mlistRedarItems.size(); i++) {
                XRadarView.RedarItem redarItem = mlistRedarItems.get(i);
                RadarPopItem item = new RadarPopItem();
                item.setColor(redarItem.getPointColor());
                item.setId(i);
                item.setName(redarItem.getName());
                item.setValue(redarItem.getPercents()[parent]);
                radarPopItems.add(item);
            }
        } else {
            XRadarView.RedarItem redarItem = mlistRedarItems.get(parent);
            RadarPopItem item = new RadarPopItem();
            item.setColor(redarItem.getPointColor());
            item.setId(child);
            item.setName(redarItem.getName());
            item.setValue(redarItem.getPercents()[child]);
            radarPopItems.add(item);
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return radarPopItems.size();
    }

    @Override
    public Object getItem(int position) {
        return radarPopItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LinearLayout.inflate(mContext, R.layout.popup_item, null);
        TextView circleView = (TextView) view.findViewById(R.id.tv_img);
        TextView name = (TextView) view.findViewById(R.id.tv_name);
        TextView value = (TextView) view.findViewById(R.id.tv_value);
        RadarPopItem item = radarPopItems.get(position);
        circleView.setBackgroundColor(item.getColor());
        if(item.getAlpha()!=-1)circleView.setAlpha(item.getAlpha()/255.f);
        name.setText(item.getName());
        value.setText(item.getValue() + "");
        return view;
    }
}
